<template>
    <el-dialog :title="`${ruleForm.id ? '编辑' : '新增'}费用配置`" :visible.sync="dialogVisible" width="25%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template lable="费用信息">
                <el-col :span="24">
                    <el-form-item label="费用类型" prop="type">
                        <el-select v-model="ruleForm.name" placeholder="请选择费用类型" style="display:block" @change="typeChange">
                            <el-option label="上牌费" value="上牌费"></el-option>
                            <el-option label="检测费" value="检测费"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="24">
                    <el-form-item label="费用" prop="fee">
                        <c-number-input v-model="ruleForm.fee" placeholder="请输入">
                            <template slot="append"> 元/台 </template>
                        </c-number-input>
                    </el-form-item>
                </el-col>
            </c-form-template>

        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    data () {
        return {
            dialogVisible: false,
            loading: false,
            ruleForm: {
                type: ""
            },
            rules: {
                type: [{ required: true, message: "请选择费用类型", trigger: "blur" }],
                fee: [{ required: true, message: "请输入金额", trigger: "blur" }],
            },
        };
    },
    methods: {
        typeChange (e) {
            switch (e) {
                case "上牌费":
                    this.ruleForm.type = 1;
                    break;
                case "检测费":
                    this.ruleForm.type = 2;
                    break;
            }
        },
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        this.$post({
                            url: `/ShopServiceFee/${data.id ? "update" : "insert"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {
            if (item && item.id) {
                this.ruleForm = {
                    ...item,
                };
            }
            this.dialogVisible = true;
            this.loading = false;
        },
        handleClose () {
            this.ruleForm = {
            };
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
